package com.yx.xinruitu.service.impl;

import com.yx.xinruitu.dao.QuestionDao;
import com.yx.xinruitu.dao.StaffDao;
import com.yx.xinruitu.entity.PaginationList;
import com.yx.xinruitu.entity.ResponseModel;
import com.yx.xinruitu.service.IQuestionService;
import com.yx.xinruitu.util.ParameterMap;
import com.yx.xinruitu.util.Tools;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.List;

@Service
public class QuestionService implements IQuestionService {

    @Autowired
	private QuestionDao questionDao;

	@Value("${upload.root.folder}")
	public String root_fold;

	@Value("${img.folder}")
	public String img_fold;

	@Value("${user.folder}")
	public String user_folder;

	private Logger log = Logger.getLogger(this.getClass());

	@Autowired
	private StaffDao  staffDao;


	@Override
	public List<ParameterMap> getTestQuestionList(ParameterMap pm){
		List<ParameterMap> list = questionDao.getAllQuestions(pm);
		return list;
	}

	@Override
	public HashMap<String, Object> getQuestionListByPage(ParameterMap pm,Integer pageSize,Integer  currIndex) {
		try {
			pm.put("currIndex",(currIndex-1)*pageSize);
			pm.put("pageSize",pageSize);
			List<ParameterMap> list=questionDao.getQuestionListByPage(pm);
			Integer allcount=questionDao.getQuestionListCount(pm);
			PaginationList page=new PaginationList();
			page.setList(list);
			page.setDraw(currIndex.longValue());
			page.setPageSize(pageSize);
			page.setRecordsTotal(allcount!=null?allcount:0);
			page.setTotalpage(page.getRecordsTotal() % pageSize > 0 ? (page.getRecordsTotal() / pageSize) + 1 : (page.getRecordsTotal() / pageSize));
			return ResponseModel.getModel("ok", "success",page);
		}catch(Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			e.printStackTrace();
			log.error("add menu error", e);
			return ResponseModel.getModel("error", "falied", null);
		}
	}


    @Override
	@Transactional
	public HashMap<String, Object> add(ParameterMap pm) {
		try {
			questionDao.saveQuestion(pm);
		} catch (Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			e.printStackTrace();
			log.error("error:"+e.getMessage(), e);
			return ResponseModel.getModel("提交失败", "failed", null);
		}
		return ResponseModel.getModel("ok", "success", null);
	}

	@Override
    @Transactional
	public HashMap<String, Object> edit(ParameterMap pm) {
		try {
			questionDao.editQuestion(pm);
		} catch (Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			e.printStackTrace();
			log.error("error:"+e.getMessage(), e);
			return ResponseModel.getModel("提交失败", "failed", null);
		}
		return ResponseModel.getModel("ok", "success", null);
	}


    @Override
    @Transactional
	public HashMap<String, Object> del(ParameterMap pm) {
		try {
			if(Tools.isEmpty(pm.getString("id"))){
				return ResponseModel.getModel("你请求的是冒牌接口", "falied", null);
			}
			String id = pm.getString("id");
			if(Tools.isEmpty(id)){
				return ResponseModel.getModel("你请求的是冒牌接口", "falied", null);
			}
			questionDao.delQuestion(id);
		} catch (Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			e.printStackTrace();
			log.error("error:"+e.getMessage(), e);
			return ResponseModel.getModel("提交失败", "failed", null);
		}
		return ResponseModel.getModel("ok", "success", null);
	}
    @Override
    @Transactional
	public HashMap<String, Object> find(ParameterMap pm) {
		try {
			String id = pm.getString("id");
			if(Tools.isEmpty(id)){
				return ResponseModel.getModel("你请求的是冒牌接口", "falied", null);
			}
			ParameterMap map=questionDao.findQuestion(id);
			return ResponseModel.getModel("ok", "success",map);
		} catch (Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			e.printStackTrace();
			log.error("error:"+e.getMessage(), e);
			return ResponseModel.getModel("error", "failed", null);
		}
	}

	@Override
	@Transactional
	public HashMap<String, Object> getDept() {
		try {
			List<ParameterMap> list=questionDao.getDept();
			return ResponseModel.getModel("ok", "success",list);
		} catch (Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			e.printStackTrace();
			log.error("error:"+e.getMessage(), e);
			return ResponseModel.getModel("error", "failed", null);
		}
	}

	@Override
	public List<ParameterMap>  getSingleQuestions(ParameterMap pm) {
		try {
			return questionDao.getSingleQuestions(pm);
		} catch (Exception e) {
			/*TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			e.printStackTrace();*/
			log.error("error:"+e.getMessage(), e);
			return null;
		}
	}

	@Override
	public List<ParameterMap>  getMultipleQuestions(ParameterMap pm) {
		try {
			List<ParameterMap> list=questionDao.getMultipleQuestions(pm);
			return list;
		} catch (Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			e.printStackTrace();
			log.error("error:"+e.getMessage(), e);
			return null;
		}
	}
}
